/**
 * 
 */
package br.gov.sjc.socialalimenta;


import android.app.AlertDialog;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
import br.gov.sjc.socialalimenta.SocialAlimenta2Activity;
import br.org.sjc.validacao.Sair;

/**
 * @author rodrigo.ramos
 *
 */
public class AcessoAlimentacaoActivity extends Sair {
	//Declara base de dados
	public static SQLiteDatabase BancoDados = null;
	public static String nomeBanco = "SocialAlimenta";
	public void openDB(){
		try{
			BancoDados = openOrCreateDatabase(nomeBanco,MODE_WORLD_READABLE,null);
			Log.v("AcessoAlimentacaoActivity", "BancoDados.openOrCreateDatabase();");
		}catch (Exception E){
			Log.e("AcessoAlimentacaoActivity", "Erro em BancoDados.close();"+E.getMessage());
		} 
	}
	public void closeDB(){
      	BancoDados.close();
     	Log.v("AcessoAlimentacaoActivity", "BancoDados.close();");
	}
	
	// Declara os Spinners
	Spinner spFreqArroz, spFormaArroz, spFreqLeite, spFormaLeite;
	Spinner spFreqDerivados, spFormaDerivados, spFreqPaes, spFormaPaes;
	Spinner spFreqManteiga, spFormaManteiga, spFreqMacarrao, spFormaMacarrao;
	Spinner spFreqFuba, spFormaFuba, spFreqFrutas, spFormaFrutas;
	Spinner spFreqVerduras, spFormaVerduras, spFreqCarne, spFormaCarne;
	Spinner spFreqOvos, spFormaOvos, spFreqFarinhas, spFormaFarinhas;
	
    //Declara base de dados
    //SQLiteDatabase BancoDados = SocialAlimenta2Activity.BancoDados ;
	
    //Pega o numero do Sias em uso atual
    	NumeroSias ns = new NumeroSias();
		int NumSias = ns.getNumeroDaFamilia();
		int ID;
		
	    //Declara Cursor
	    Cursor cVerifica;
	    
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.acessoalimentacao);
        registerBaseActivityReceiver();
        openDB();
           	
        /*****************************
         * Configuração dos Spinners
         * *************************** */
        //spFreqArroz
         spFreqArroz = (Spinner) findViewById(R.acessoAlimentacao.spfreqArroz);
        ArrayAdapter<CharSequence> adpFreqArroz = ArrayAdapter.createFromResource(
        this, R.array.Frequencia,
        android.R.layout.simple_spinner_item);
        adpFreqArroz.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spFreqArroz.setAdapter(adpFreqArroz);
        
         spFormaArroz = (Spinner) findViewById(R.acessoAlimentacao.spFormaArroz);
        ArrayAdapter<CharSequence> adpFormaArroz = ArrayAdapter.createFromResource(
        this, R.array.FormaAcesso,
        android.R.layout.simple_spinner_item);
        adpFormaArroz.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spFormaArroz.setAdapter(adpFormaArroz);
      //spFreqLeite
         spFreqLeite = (Spinner) findViewById(R.acessoAlimentacao.spFreqLeite);
        ArrayAdapter<CharSequence> adpFreqLeite = ArrayAdapter.createFromResource(
        this, R.array.Frequencia,
        android.R.layout.simple_spinner_item);
        adpFreqLeite.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spFreqLeite.setAdapter(adpFreqLeite);
 
         spFormaLeite = (Spinner) findViewById(R.acessoAlimentacao.spFormaLeite);
        ArrayAdapter<CharSequence> adpFormaLeite = ArrayAdapter.createFromResource(
        this, R.array.FormaAcesso,
        android.R.layout.simple_spinner_item);
        adpFormaLeite.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spFormaLeite.setAdapter(adpFormaLeite);
      //spFreqDerivados
         spFreqDerivados = (Spinner) findViewById(R.acessoAlimentacao.spFreqDerivados);
        ArrayAdapter<CharSequence> adpFreqDerivados = ArrayAdapter.createFromResource(
        this, R.array.Frequencia,
        android.R.layout.simple_spinner_item);
        adpFreqDerivados.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spFreqDerivados.setAdapter(adpFreqDerivados);
        
         spFormaDerivados = (Spinner) findViewById(R.acessoAlimentacao.spFormaDerivados);
        ArrayAdapter<CharSequence> adpFormaDerivados = ArrayAdapter.createFromResource(
        this, R.array.FormaAcesso,
        android.R.layout.simple_spinner_item);
        adpFormaDerivados.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spFormaDerivados.setAdapter(adpFormaDerivados);
      //spFreqPaes
         spFreqPaes = (Spinner) findViewById(R.acessoAlimentacao.spFreqPaes);
        ArrayAdapter<CharSequence> adpFreqPaes = ArrayAdapter.createFromResource(
        this, R.array.Frequencia,
        android.R.layout.simple_spinner_item);
        adpFreqPaes.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spFreqPaes.setAdapter(adpFreqPaes);
        
         spFormaPaes = (Spinner) findViewById(R.acessoAlimentacao.spFormaPaes);
        ArrayAdapter<CharSequence> adpFormaPaes = ArrayAdapter.createFromResource(
        this, R.array.FormaAcesso,
        android.R.layout.simple_spinner_item);
        adpFormaPaes.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spFormaPaes.setAdapter(adpFormaPaes);
      //spFormaManteiga
         spFreqManteiga = (Spinner) findViewById(R.acessoAlimentacao.spFreqManteiga);
        ArrayAdapter<CharSequence> adpFreqManteiga = ArrayAdapter.createFromResource(
        this, R.array.Frequencia,
        android.R.layout.simple_spinner_item);
        adpFreqManteiga.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spFreqManteiga.setAdapter(adpFreqManteiga);
        
         spFormaManteiga = (Spinner) findViewById(R.acessoAlimentacao.spFormaManteiga);
        ArrayAdapter<CharSequence> adpFormaManteiga = ArrayAdapter.createFromResource(
        this, R.array.FormaAcesso,
        android.R.layout.simple_spinner_item);
        adpFormaManteiga.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spFormaManteiga.setAdapter(adpFormaManteiga);
      //spFreqMacarrao
         spFreqMacarrao = (Spinner) findViewById(R.acessoAlimentacao.spFreqMacarrao);
        ArrayAdapter<CharSequence> adpFreqMacarrao = ArrayAdapter.createFromResource(
        this, R.array.Frequencia,
        android.R.layout.simple_spinner_item);
        adpFreqMacarrao.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spFreqMacarrao.setAdapter(adpFreqMacarrao);
        
         spFormaMacarrao = (Spinner) findViewById(R.acessoAlimentacao.spFormaMacarrao);
        ArrayAdapter<CharSequence> adpFormaMacarrao = ArrayAdapter.createFromResource(
        this, R.array.FormaAcesso,
        android.R.layout.simple_spinner_item);
        adpFormaMacarrao.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spFormaMacarrao.setAdapter(adpFormaMacarrao);
        //Fuba
         spFreqFuba = (Spinner) findViewById(R.acessoAlimentacao.spFreqFuba);
        ArrayAdapter<CharSequence> adpFreqFuba = ArrayAdapter.createFromResource(
        this, R.array.Frequencia,
        android.R.layout.simple_spinner_item);
        adpFreqFuba.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spFreqFuba.setAdapter(adpFreqFuba);
        
         spFormaFuba = (Spinner) findViewById(R.acessoAlimentacao.spFormaFuba);
        ArrayAdapter<CharSequence> adpFormaFuba = ArrayAdapter.createFromResource(
        this, R.array.FormaAcesso,
        android.R.layout.simple_spinner_item);
        adpFormaFuba.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spFormaFuba.setAdapter(adpFormaFuba);        
        //Frutas
         spFreqFrutas = (Spinner) findViewById(R.acessoAlimentacao.spFreqFrutas);
        ArrayAdapter<CharSequence> adpFreqFrutas = ArrayAdapter.createFromResource(
        this, R.array.Frequencia,
        android.R.layout.simple_spinner_item);
        adpFreqFrutas.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spFreqFrutas.setAdapter(adpFreqFrutas);
        
         spFormaFrutas = (Spinner) findViewById(R.acessoAlimentacao.spFormaFrutas);
        ArrayAdapter<CharSequence> adpFormaFrutas = ArrayAdapter.createFromResource(
        this, R.array.FormaAcesso,
        android.R.layout.simple_spinner_item);
        adpFormaFrutas.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spFormaFrutas.setAdapter(adpFormaFrutas);          
        //Verduras
         spFreqVerduras = (Spinner) findViewById(R.acessoAlimentacao.spFreqVerduras);
        ArrayAdapter<CharSequence> adpFreqVerduras = ArrayAdapter.createFromResource(
        this, R.array.Frequencia,
        android.R.layout.simple_spinner_item);
        adpFreqVerduras.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spFreqVerduras.setAdapter(adpFreqVerduras);
        
         spFormaVerduras = (Spinner) findViewById(R.acessoAlimentacao.spFormaVerduras);
        ArrayAdapter<CharSequence> adpFormaVerduras = ArrayAdapter.createFromResource(
        this, R.array.FormaAcesso,
        android.R.layout.simple_spinner_item);
        adpFormaVerduras.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spFormaVerduras.setAdapter(adpFormaVerduras);          
        //Carne
         spFreqCarne = (Spinner) findViewById(R.acessoAlimentacao.spFreqCarne);
        ArrayAdapter<CharSequence> adpFreqCarne = ArrayAdapter.createFromResource(
        this, R.array.Frequencia,
        android.R.layout.simple_spinner_item);
        adpFreqCarne.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spFreqCarne.setAdapter(adpFreqCarne);
        
         spFormaCarne = (Spinner) findViewById(R.acessoAlimentacao.spFormaCarne);
        ArrayAdapter<CharSequence> adpFormaCarne = ArrayAdapter.createFromResource(
        this, R.array.FormaAcesso,
        android.R.layout.simple_spinner_item);
        adpFormaCarne.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spFormaCarne.setAdapter(adpFormaCarne);          
        //Ovos
         spFreqOvos = (Spinner) findViewById(R.acessoAlimentacao.spFreqOvos);
        ArrayAdapter<CharSequence> adpFreqOvos = ArrayAdapter.createFromResource(
        this, R.array.Frequencia,
        android.R.layout.simple_spinner_item);
        adpFreqOvos.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spFreqOvos.setAdapter(adpFreqOvos);
        
         spFormaOvos = (Spinner) findViewById(R.acessoAlimentacao.spFormaOvos);
        ArrayAdapter<CharSequence> adpFormaOvos = ArrayAdapter.createFromResource(
        this, R.array.FormaAcesso,
        android.R.layout.simple_spinner_item);
        adpFormaOvos.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spFormaOvos.setAdapter(adpFormaOvos);  
        //Farinhas
        spFreqFarinhas = (Spinner) findViewById(R.acessoAlimentacao.spFreqFarinhas);
        ArrayAdapter<CharSequence> adpFreqFarinhas = ArrayAdapter.createFromResource(
        this, R.array.Frequencia,
        android.R.layout.simple_spinner_item);
        adpFreqFarinhas.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spFreqFarinhas.setAdapter(adpFreqFarinhas);
       
         spFormaFarinhas = (Spinner) findViewById(R.acessoAlimentacao.spFormaFarinhas);
        ArrayAdapter<CharSequence> adpFormaFarinhas = ArrayAdapter.createFromResource(
        this, R.array.FormaAcesso,
        android.R.layout.simple_spinner_item);
        adpFormaFarinhas.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spFormaFarinhas.setAdapter(adpFormaFarinhas);  
        
        
        /** **********************************
         *  Fim da Configuração dos Spinners
         ** ********************************** */
        
        
        //this.dh = new DataHelper(this);
        
        // Declara Buttons
        Button btnAAVoltar, btnAAIr;
        
        /** Inicia o set de dados caso haja dados */
        if (verificaRegistro(NumSias)){
        	setarDados(ID);
        	Log.v("AcessoAlimentacaoActivity", "onCreate() Com dados do SiasN: "+NumSias+"ID: "+ID);
        }else{
        	//Num tem seNão
        	Log.v("AcessoAlimentacaoActivity", "onCreate() Com dados vazios ");
        }
        
        /** Acao do botao Voltar */
        btnAAVoltar = (Button) findViewById(R.acessoAlimentacao.btnVoltar);
        btnAAVoltar.setOnClickListener(new View.OnClickListener() {
            
            public void onClick(View arg0) {
                /** InitComponents */
                Intent ittAAVoltar = new Intent(getApplicationContext(), IndSegAlimentarActivity.class);
                closeDB();
                startActivity(ittAAVoltar);
            }
        });//fim acao do botao voltar
        
        /** Acao do botao Salvar */
        btnAAIr = (Button) findViewById(R.acessoAlimentacao.btnSalvar);
        btnAAIr.setOnClickListener(new View.OnClickListener() {
            
            public void onClick(View arg0) {
             	  if (verificaRegistro(NumSias)){
             		  alteraRegistro(ID);
    	       	  }else{
    	        	//salva dados
                salvaDados(NumSias+"", spFreqArroz.getSelectedItem().toString(), spFormaArroz.getSelectedItem().toString(), spFreqLeite.getSelectedItem().toString(), spFormaLeite.getSelectedItem().toString(),
            	 spFreqDerivados.getSelectedItem().toString(), spFormaDerivados.getSelectedItem().toString(), spFreqPaes.getSelectedItem().toString(), spFormaPaes.getSelectedItem().toString(),
            	 spFreqManteiga.getSelectedItem().toString(), spFormaManteiga.getSelectedItem().toString(), spFreqMacarrao.getSelectedItem().toString(), spFormaMacarrao.getSelectedItem().toString(),
            	 spFreqFuba.getSelectedItem().toString(), spFormaFuba.getSelectedItem().toString(), spFreqFrutas.getSelectedItem().toString(), spFormaFrutas.getSelectedItem().toString(),
            	 spFreqVerduras.getSelectedItem().toString(), spFormaVerduras.getSelectedItem().toString(), spFreqCarne.getSelectedItem().toString(), spFormaCarne.getSelectedItem().toString(),
            	 spFreqOvos.getSelectedItem().toString(), spFormaOvos.getSelectedItem().toString(), spFreqFarinhas.getSelectedItem().toString(),spFormaFarinhas.getSelectedItem().toString()
                		);
    	       	  }
             	  
                /** InitComponents */
                Intent ittAAIr = new Intent(getApplicationContext(), SocialAlimenta2Activity.class);
                closeDB();
                startActivity(ittAAIr);
                
            }
        });//fim acao do botao Salvar
    }
    
    //CREATE TABLE IF NOT EXISTS tab_SocialA_AcessoAlimen(_id INTEGER PRIMARY KEY, SiasN INT, ArrozFrequencia TEXT,ArrozFormaAcesso TEXT, LeiteFrequencia TEXT,LeiteFormaAcesso TEXT, DerivadosFrequencia TEXT,DerivadosFormaAcesso TEXT, PaesFrequencia TEXT,PaesFormaAcesso TEXT, ManteigaFrequencia TEXT,ManteigaFormaAcesso TEXT, MacarraoFrequencia TEXT,MacarraoFormaAcesso TEXT, FubaFrequencia TEXT,FubaFormaAcesso TEXT, FrutasFrequencia TEXT, FrutasFormaAcesso TEXT, VerdurasFrequencia TEXT,VerdurasFormaAcesso TEXT, CarneFrequencia TEXT,CarneFormaAcesso TEXT, OvosFrequencia TEXT,OvosFormaAcesso TEXT, FarinhasFrequencia TEXT,FarinhasFormaAcesso TEXT)";
	
	public void salvaDados(String Var1, String Var2, String Var3, String Var4, String Var5, String Var6, String Var7, String Var8, String Var9, String Var10
			, String Var11, String Var12, String Var13, String Var14, String Var15, String Var16, String Var17, String Var18, String Var19, String Var20
			, String Var21, String Var22, String Var23, String Var24, String Var25){
		try{
			String SQL = "INSERT INTO tab_SocialA_AcessoAlimen (SiasN , ArrozFrequencia ,ArrozFormaAcesso , LeiteFrequencia ,LeiteFormaAcesso , DerivadosFrequencia ,DerivadosFormaAcesso , PaesFrequencia ,PaesFormaAcesso , ManteigaFrequencia ,ManteigaFormaAcesso , MacarraoFrequencia ,MacarraoFormaAcesso , FubaFrequencia ,FubaFormaAcesso , FrutasFrequencia , FrutasFormaAcesso , VerdurasFrequencia ,VerdurasFormaAcesso , CarneFrequencia ,CarneFormaAcesso , OvosFrequencia ,OvosFormaAcesso , FarinhasFrequencia ,FarinhasFormaAcesso) "+
		"VALUES ("+Var1+", '"+Var2+"', '"+Var3+"', '"+Var4+"', '"+Var5+"', '"+Var6+"', '"+Var7+"', '"+Var8+"', '"+Var9+"', '"+Var10+"'" +
				", '"+Var11+"', '"+Var12+"', '"+Var13+"', '"+Var14+"', '"+Var15+"', '"+Var16+"', '"+Var17+"', '"+Var18+"', '"+Var19+"', '"+Var20+"'" +
						", '"+Var21+"', '"+Var22+"', '"+Var23+"', '"+Var24+"', '"+Var25+"')";
			
			BancoDados.execSQL(SQL);
			//mensagemAlerta("Ok","Dados inseridos corretamente - "+SQL);
			Toast.makeText(this, "Gravando Dados da Família:"+NumSias, Toast.LENGTH_LONG).show();
		}catch(Exception erro) {
			mensagemAlerta("Erro","Erro Ao Gravar ");
			Log.e("AcessoAlimentacaoActivity", "salvaDados() ERRO: "+erro.getMessage());
		}
	}
	// Declara os Spinners

	
	public boolean verificaRegistro(int NumeroSias){
		boolean Retorno = false;
		try{
			String SQLQuery = "Select * from tab_SocialA_AcessoAlimen WHERE SiasN="+NumeroSias+" ";
			cVerifica = BancoDados.rawQuery(SQLQuery, null);
			if (cVerifica.getCount() !=0 ){
				cVerifica.moveToFirst();
				ID = cVerifica.getInt(cVerifica.getColumnIndex("_id"));
				Retorno = true;
				Log.i("AcessoAlimentacaoActivity", "verificaRegistro() TRUE ");
			}
		}catch (Exception Err){
			//mensagemAlerta("Erro","Erro Encontrado:"+Err.getMessage());
			Log.e("AcessoAlimentacaoActivity", "verificaRegistro() "+Err.getMessage());
		}
		return Retorno;
	}
	   public void setarDados(int Posicao){
	    	try {
	    		String SQL2 = "SELECT * FROM tab_SocialA_AcessoAlimen WHERE _id="+Posicao+" ";
	    		Log.v("AcessoAlimentacaoActivity", "setarDados() Posição: "+Posicao);
	    		Cursor c = BancoDados.rawQuery(SQL2, null);
	    		c.moveToFirst();
	    		 
	    		do {
	    			/**Spinner spFreqArroz, spFormaArroz, spFreqLeite, spFormaLeite;
	    			Spinner spFreqDerivados, spFormaDerivados, spFreqPaes, spFormaPaes;*/
	    			  //spFreqArroz
	    			ArrayAdapter<CharSequence> adpUFreqArroz = ArrayAdapter.createFromResource(
	    			        this, R.array.Frequencia,
	    			        android.R.layout.simple_spinner_item);
	    			adpUFreqArroz.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    			int iUFreqArroz = adpUFreqArroz.getPosition(c.getString(c.getColumnIndex("ArrozFrequencia")));
	    			spFreqArroz.setSelection(iUFreqArroz);
	    			
	    	        //spFormaArroz
	    			ArrayAdapter<CharSequence> adpUFormaArroz = ArrayAdapter.createFromResource(
	    			        this, R.array.FormaAcesso,
	    			        android.R.layout.simple_spinner_item);
	    			adpUFormaArroz.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    			int iUFormaArroz = adpUFormaArroz.getPosition(c.getString(c.getColumnIndex("ArrozFormaAcesso")));
	    			spFormaArroz.setSelection(iUFormaArroz);

	    	        //spFreqLeite
	    			ArrayAdapter<CharSequence> adpUFreqLeite = ArrayAdapter.createFromResource(
	    			        this, R.array.Frequencia,
	    			        android.R.layout.simple_spinner_item);
	    			adpUFreqLeite.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    			int iUFreqLeite = adpUFreqLeite.getPosition(c.getString(c.getColumnIndex("LeiteFrequencia")));
	    			spFreqLeite.setSelection(iUFreqLeite);
	    			
	    	        //spFormaLeite
	    			ArrayAdapter<CharSequence> adpUFormaLeite = ArrayAdapter.createFromResource(
	    			        this, R.array.FormaAcesso,
	    			        android.R.layout.simple_spinner_item);
	    			adpUFormaLeite.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    			int iUFormaLeite = adpUFormaLeite.getPosition(c.getString(c.getColumnIndex("LeiteFormaAcesso")));
	    			spFormaLeite.setSelection(iUFormaLeite);
	    			
	    	        //spFreqDerivados
	    			ArrayAdapter<CharSequence> adpUFreqDerivados = ArrayAdapter.createFromResource(
	    			        this, R.array.Frequencia,
	    			        android.R.layout.simple_spinner_item);
	    			adpUFreqDerivados.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    			int iUFreqDerivados = adpUFreqDerivados.getPosition(c.getString(c.getColumnIndex("DerivadosFrequencia")));
	    			spFreqLeite.setSelection(iUFreqDerivados);
	    			
	    	        //spFormaDerivados
	    			ArrayAdapter<CharSequence> adpUFormaDerivados = ArrayAdapter.createFromResource(
	    			        this, R.array.FormaAcesso,
	    			        android.R.layout.simple_spinner_item);
	    			adpUFormaDerivados.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    			int iUFormaDerivados = adpUFormaDerivados.getPosition(c.getString(c.getColumnIndex("DerivadosFormaAcesso")));
	    			spFormaDerivados.setSelection(iUFormaDerivados);
	    			
	    	        //spFreqPaes
	    			ArrayAdapter<CharSequence> adpUFreqPaes = ArrayAdapter.createFromResource(
	    			        this, R.array.Frequencia,
	    			        android.R.layout.simple_spinner_item);
	    			adpUFreqPaes.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    			int iUFreqPaes = adpUFreqPaes.getPosition(c.getString(c.getColumnIndex("PaesFrequencia")));
	    			spFreqPaes.setSelection(iUFreqPaes);
	    			
	    	        //spFormaPaes
	    			ArrayAdapter<CharSequence> adpUFormaPaes = ArrayAdapter.createFromResource(
	    			        this, R.array.FormaAcesso,
	    			        android.R.layout.simple_spinner_item);
	    			adpUFormaPaes.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    			int iUFormaPaes = adpUFormaPaes.getPosition(c.getString(c.getColumnIndex("PaesFormaAcesso")));
	    			spFormaPaes.setSelection(iUFormaPaes);
    	      
	    	        //spFreqManteiga
	    			ArrayAdapter<CharSequence> adpUFreqManteiga = ArrayAdapter.createFromResource(
	    			        this, R.array.Frequencia,
	    			        android.R.layout.simple_spinner_item);
	    			adpUFreqManteiga.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    			int iUFreqManteiga= adpUFreqManteiga.getPosition(c.getString(c.getColumnIndex("ManteigaFrequencia")));
	    			spFreqManteiga.setSelection(iUFreqManteiga);
	    			
	    	        //spFormaManteiga
	    			ArrayAdapter<CharSequence> adpUFormaManteiga = ArrayAdapter.createFromResource(
	    			        this, R.array.FormaAcesso,
	    			        android.R.layout.simple_spinner_item);
	    			adpUFormaManteiga.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    			int iUFormaManteiga = adpUFormaManteiga.getPosition(c.getString(c.getColumnIndex("ManteigaFormaAcesso")));
	    			spFormaManteiga.setSelection(iUFormaManteiga);

	    	        //spFreqMacarrao
	    			ArrayAdapter<CharSequence> adpUFreqMacarrao= ArrayAdapter.createFromResource(
	    			        this, R.array.Frequencia,
	    			        android.R.layout.simple_spinner_item);
	    			adpUFreqMacarrao.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    			int iUFreqMacarrao= adpUFreqMacarrao.getPosition(c.getString(c.getColumnIndex("MacarraoFrequencia")));
	    			spFreqMacarrao.setSelection(iUFreqMacarrao);
	    			
	    	        //spFormaMMacarrao
	    			ArrayAdapter<CharSequence> adpUFormaMacarrao = ArrayAdapter.createFromResource(
	    			        this, R.array.FormaAcesso,
	    			        android.R.layout.simple_spinner_item);
	    			adpUFormaMacarrao.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    			int iUFormaMacarrao = adpUFormaMacarrao.getPosition(c.getString(c.getColumnIndex("MacarraoFormaAcesso")));
	    			spFormaMacarrao.setSelection(iUFormaMacarrao);
	    			
	    	        //spFreqFuba
	    			ArrayAdapter<CharSequence> adpUFreqFuba= ArrayAdapter.createFromResource(
	    			        this, R.array.Frequencia,
	    			        android.R.layout.simple_spinner_item);
	    			adpUFreqFuba.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    			int iUFreqFuba= adpUFreqFuba.getPosition(c.getString(c.getColumnIndex("FubaFrequencia")));
	    			spFreqFuba.setSelection(iUFreqFuba);
	    			
	    	        //spFormaFuba
	    			ArrayAdapter<CharSequence> adpUFormaFuba = ArrayAdapter.createFromResource(
	    			        this, R.array.FormaAcesso,
	    			        android.R.layout.simple_spinner_item);
	    			adpUFormaFuba.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    			int iUFormaFuba = adpUFormaFuba.getPosition(c.getString(c.getColumnIndex("FubaFormaAcesso")));
	    			spFormaFuba.setSelection(iUFormaFuba);
	    			
	    	        //spFreqFrutas
	    			ArrayAdapter<CharSequence> adpUFreqFrutas= ArrayAdapter.createFromResource(
	    			        this, R.array.Frequencia,
	    			        android.R.layout.simple_spinner_item);
	    			adpUFreqFrutas.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    			int iUFreqFrutas= adpUFreqFrutas.getPosition(c.getString(c.getColumnIndex("FrutasFrequencia")));
	    			spFreqFrutas.setSelection(iUFreqFrutas);
	    			
	    	        //spFormaFrutas
	    			ArrayAdapter<CharSequence> adpUFormaFrutas = ArrayAdapter.createFromResource(
	    			        this, R.array.FormaAcesso,
	    			        android.R.layout.simple_spinner_item);
	    			adpUFormaFrutas.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    			int iUFormaFrutas = adpUFormaFrutas.getPosition(c.getString(c.getColumnIndex("FrutasFormaAcesso")));
	    			spFormaFrutas.setSelection(iUFormaFrutas);
	    			
	    	        //spFreqVerduras
	    			ArrayAdapter<CharSequence> adpUFreqVerduras = ArrayAdapter.createFromResource(
	    			        this, R.array.Frequencia,
	    			        android.R.layout.simple_spinner_item);
	    			adpUFreqVerduras.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    			int iUFreqVerduras = adpUFreqVerduras.getPosition(c.getString(c.getColumnIndex("VerdurasFrequencia")));
	    			spFreqVerduras.setSelection(iUFreqVerduras);
	    			
	    	        //spFormaVerduras
	    			ArrayAdapter<CharSequence> adpUFormaVerduras = ArrayAdapter.createFromResource(
	    			        this, R.array.FormaAcesso,
	    			        android.R.layout.simple_spinner_item);
	    			adpUFormaVerduras.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    			int iUFormaVerduras = adpUFormaVerduras.getPosition(c.getString(c.getColumnIndex("FrutasFormaAcesso")));
	    			spFormaVerduras.setSelection(iUFormaVerduras);
	    			
	    	        //spFreqCarne
	    			ArrayAdapter<CharSequence> adpUFreqCarne = ArrayAdapter.createFromResource(
	    			        this, R.array.Frequencia,
	    			        android.R.layout.simple_spinner_item);
	    			adpUFreqCarne.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    			int iUFreqCarne = adpUFreqCarne.getPosition(c.getString(c.getColumnIndex("CarneFrequencia")));
	    			spFreqCarne.setSelection(iUFreqCarne);
	    			
	    	        //spFormaCarne
	    			ArrayAdapter<CharSequence> adpUFormaCarne = ArrayAdapter.createFromResource(
	    			        this, R.array.FormaAcesso,
	    			        android.R.layout.simple_spinner_item);
	    			adpUFormaCarne.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    			int iUFormaCarne = adpUFormaCarne.getPosition(c.getString(c.getColumnIndex("CarneFormaAcesso")));
	    			spFormaCarne.setSelection(iUFormaCarne);
	    			
	    	        //spFreqOvos
	    			ArrayAdapter<CharSequence> adpUFreqOvos = ArrayAdapter.createFromResource(
	    			        this, R.array.Frequencia,
	    			        android.R.layout.simple_spinner_item);
	    			adpUFreqOvos.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    			int iUFreqOvos = adpUFreqOvos.getPosition(c.getString(c.getColumnIndex("OvosFrequencia")));
	    			spFreqOvos.setSelection(iUFreqOvos);
	    			
	    	        //spFormaOvos
	    			ArrayAdapter<CharSequence> adpUFormaOvos = ArrayAdapter.createFromResource(
	    			        this, R.array.FormaAcesso,
	    			        android.R.layout.simple_spinner_item);
	    			adpUFormaOvos.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    			int iUFormaOvos = adpUFormaOvos.getPosition(c.getString(c.getColumnIndex("OvosFormaAcesso")));
	    			spFormaOvos.setSelection(iUFormaOvos);
	    			
	    	        //spFreqFarinhas
	    			ArrayAdapter<CharSequence> adpUFreqFarinhas = ArrayAdapter.createFromResource(
	    			        this, R.array.Frequencia,
	    			        android.R.layout.simple_spinner_item);
	    			adpUFreqFarinhas.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    			int iUFreqFarinhas = adpUFreqFarinhas.getPosition(c.getString(c.getColumnIndex("FarinhasFrequencia")));
	    			spFreqFarinhas.setSelection(iUFreqFarinhas);
	    			
	    	        //spFormaFarinhas
	    			ArrayAdapter<CharSequence> adpUFormaFarinhas = ArrayAdapter.createFromResource(
	    			        this, R.array.FormaAcesso,
	    			        android.R.layout.simple_spinner_item);
	    			adpUFormaFarinhas.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    			int iUFormaFarinhas = adpUFormaFarinhas.getPosition(c.getString(c.getColumnIndex("FarinhasFormaAcesso")));
	    			spFormaFarinhas.setSelection(iUFormaFarinhas);

	    			}while(c.moveToNext());

	    		Log.v("AcessoAlimentacaoActivity", "setarDados() TRUE ");
	    	}catch (Exception E){
	    		mensagemAlerta("Erro","Ocorreu um erro inesperado");
	    		Log.e("AcessoAlimentacaoActivity", "Erro em setarDados() "+E.getMessage());
	    	}
	    }

	    public void alteraRegistro(int id){
	    	try{
	    		String uSQL = "UPDATE tab_SocialA_AcessoAlimen SET " +
	    				" ArrozFrequencia='"+spFreqArroz.getSelectedItem().toString()+"'," +
	    				" ArrozFormaAcesso='"+spFormaArroz.getSelectedItem().toString()+"'," +
	    				" LeiteFrequencia='"+spFreqLeite.getSelectedItem().toString()+"'," +
	    				" LeiteFormaAcesso='"+spFormaLeite.getSelectedItem().toString()+"'," +
	    				" DerivadosFrequencia='"+spFreqDerivados.getSelectedItem().toString()+"'," +
	    				" DerivadosFormaAcesso='"+spFormaDerivados.getSelectedItem().toString()+"'," +
	    				" PaesFrequencia='"+spFreqPaes.getSelectedItem().toString()+"'," +
	    				" PaesFormaAcesso='"+spFormaPaes.getSelectedItem().toString()+"'," +
	    				" ManteigaFrequencia='"+spFreqManteiga.getSelectedItem().toString()+"'," +
	    				" ManteigaFormaAcesso='"+spFormaManteiga.getSelectedItem().toString()+"'," +
	    				" MacarraoFrequencia='"+spFreqMacarrao.getSelectedItem().toString()+"'," +
	    				" MacarraoFormaAcesso='"+spFormaMacarrao.getSelectedItem().toString()+"'," +
	    				" FubaFrequencia='"+spFreqFuba.getSelectedItem().toString()+"'," +
	    				" FubaFormaAcesso='"+spFormaFuba.getSelectedItem().toString()+"'," +
	    				" FrutasFrequencia='"+spFreqFrutas.getSelectedItem().toString()+"'," +
	    				" FrutasFormaAcesso='"+spFormaFrutas.getSelectedItem().toString()+"'," +
	    				" VerdurasFrequencia='"+spFreqVerduras.getSelectedItem().toString()+"'," +
	    				" VerdurasFormaAcesso='"+spFormaVerduras.getSelectedItem().toString()+"'," +
	    				" CarneFrequencia='"+spFreqCarne.getSelectedItem().toString()+"'," +
	    				" CarneFormaAcesso='"+spFormaCarne.getSelectedItem().toString()+"'," +
	    				" OvosFrequencia='"+spFreqOvos.getSelectedItem().toString()+"'," +
	    				" OvosFormaAcesso='"+spFormaOvos.getSelectedItem().toString()+"'," +
	    				" FarinhasFrequencia='"+spFreqFarinhas.getSelectedItem().toString()+"'," +
	    				" FarinhasFormaAcesso='"+spFormaFarinhas.getSelectedItem().toString()+"'" +
	    				" WHERE _id="+id+" ";
	    		
	    			// Declara os Spinners
		    		/*Spinner spFreqArroz, spFormaArroz, spFreqLeite, spFormaLeite;
		    		Spinner spFreqDerivados, spFormaDerivados, spFreqPaes, spFormaPaes;
		    		Spinner spFreqManteiga, spFormaManteiga, spFreqMacarrao, spFormaMacarrao;
		    		Spinner spFreqFuba, spFormaFuba, spFreqFrutas, spFormaFrutas;
		    		Spinner spFreqVerduras, spFormaVerduras, spFreqCarne, spFormaCarne;
		    		Spinner spFreqOvos, spFormaOvos, spFreqFarinhas, spFormaFarinhas;*/
	    		   //SiasN , ArrozFrequencia ,ArrozFormaAcesso , LeiteFrequencia ,LeiteFormaAcesso , 
	    		   //DerivadosFrequencia ,DerivadosFormaAcesso , PaesFrequencia ,PaesFormaAcesso , 
	    		   //ManteigaFrequencia ,ManteigaFormaAcesso , MacarraoFrequencia ,MacarraoFormaAcesso , 
	    		   //FubaFrequencia ,FubaFormaAcesso , FrutasFrequencia , FrutasFormaAcesso , 
	    		   //VerdurasFrequencia ,VerdurasFormaAcesso , CarneFrequencia ,
	    		   //CarneFormaAcesso , OvosFrequencia ,OvosFormaAcesso , FarinhasFrequencia ,FarinhasFormaAcesso
	    		
	    		BancoDados.execSQL(uSQL);
	    		Toast.makeText(AcessoAlimentacaoActivity.this, "Alterado", Toast.LENGTH_LONG).show();
	    		Log.v("AcessoAlimentacaoActivity", "alteraRegistro() realizado com sucesso ");
	    	}catch(Exception Err){
	    		mensagemAlerta("Erro","Ocorreu um erro inesperado");
	    		Log.e("AcessoAlimentacaoActivity", "alteraRegistro() erro: "+Err.getMessage());
	    	}
	    }

	
	public void mensagemAlerta(String AlertTitle, String MSGAlert){
		AlertDialog.Builder Mensagem = new AlertDialog.Builder(AcessoAlimentacaoActivity.this);
		Mensagem.setTitle(AlertTitle);
		Mensagem.setMessage(MSGAlert);
		Mensagem.setNeutralButton("Ok", null);
		Mensagem.show();
	}
	@Override
	protected void onResume() {
		//quando a Activity main receber o foco novamente abre-se novamente a conexão
		openDB();
		//método responsável pela atualiza da lista de dados na tela
		//adapter.notifyDataSetChanged();
		super.onResume();
	}
	@Override
	protected void onPause() {

		//toda vez que o programa peder o foco fecha-se a conexão com o BD
		closeDB();
		super.onPause();
	}
    @Override
    protected void onDestroy() {
    	super.onDestroy();
    	unRegisterBaseActivityReceiver();
    	finish();
    }
}
